﻿package com.powerunion.extjs.utils;

import javax.servlet.http.HttpServletRequest;

public class Parameter {
	public int pageStart;
	public int pageSize;
	public String sortColumn;
	public String sortDir;
	public String order = "";
	public String sql;
	public String[][] params = null;
	public String sqlflg;
//	protected static final Logger logger = Logger.getLogger("Parameter.class");
	
	public Parameter(HttpServletRequest request, String sql){
		String s = request.getParameter("params");
		this.pageStart 	= Integer.parseInt(request.getParameter("start"));
		this.pageSize = Integer.parseInt(request.getParameter("limit"));
		this.sortColumn = request.getParameter("sort");
		this.sortDir = request.getParameter("dir");
		this.order = this.sortColumn == null ? "" : " order by " + this.sortColumn + " " + this.sortDir;
		if(s != null){
			String seg = "<separator>";
			String[] p = s.split(seg);
			String pWhere = p[0];//参数sql
			String[] pValue = p[1].split(",");	//参数值
			String[] pType = p[2].split(",");	//参数值类型
			if(pValue.length == 0) pValue = new String[pType.length];
			this.params = new String[][]{pValue, pType};
//			if(sql.toLowerCase().indexOf("where")<0){
			if(pWhere.trim().equals("")){ //如果没有where语句，直接返回
				this.sql = sql;
				return;
			}
			
			if(sql.indexOf("where")<0){
				pWhere = " where " + pWhere;
			}
			else{
				pWhere = " and (" + pWhere + ")";
			}
			this.sql = sql + pWhere + this.order;
		}
		else{
			this.sql = sql + this.order;
		}

	}

}
